﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using HRM.BusinessLogic;
namespace HRM
{
    public partial class frmNhanSu : FormBase
    {
        Employee_Info _employeeInfo;
        EmployeeClient _wcfEployee;
        HospitalClient _wcfHospital;
        HospitalInfo _HospitalInfo;
        BranchClient _wcfBranch;
        DetailClient _wcfDetail;
        LabourContract_Info _LabourInfo;
        PositionDetail_Info _PDInfo;
        int _LCID = 0;
        int _PDID = 0;
        int _BranchID = 0;
        int _departmentID = 0;
        int _EmployeeID = 0;
        int itemCount = 0;
        int pageSize = UIParamater.pageSize;
        int totalPage = 0;
        int currPage = 0;
        string _OrderBy = "";
        string _OrderDirection = "";

        public frmNhanSu(int _EmployeeID = 0)
        {
            InitializeComponent();
            _wcfEployee = Service.Employee();
            _employeeInfo = new Employee_Info();
            _wcfBranch = Service.Branch();
            this._EmployeeID = _EmployeeID;
            _wcfHospital = Service.Hospital();
            _wcfDetail = Service.Detail();
            _LabourInfo = new LabourContract_Info();
            _PDInfo = new PositionDetail_Info();
            DateTextBox dt1 = new DateTextBox(txtEmployee_Birthday);
            DateTextBox dt2 = new DateTextBox(txtEmployee_IDcardDate);
            DateTextBox dt3 = new DateTextBox(txtDateStart);
            DateTextBox dt4 = new DateTextBox(txtLC_Date);
            DateTextBoxShort dt5 = new DateTextBoxShort(txtDateSalary);
            DateTextBoxShort dt6 = new DateTextBoxShort(txtDatePC);
            DateTextBoxShort dt7 = new DateTextBoxShort(txtDateTNVK);
            DateTextBoxShort dt8 = new DateTextBoxShort(txtDateTNN);
            DateTextBoxShort dt9 = new DateTextBoxShort(txtPD_BeginDate);
            FormGlobals.Combobox_SetAutoComplete(cboEmployee_Ethnic, cboNational, cboDept, cboContract_Type, cboPost);
            LoadEthnic();
            LoadNational();
            txtEmployee_HospitalRegionCode.Text = Program.strRegionCode;
            UISetting.LoadPositionByBranchCode(ref cboPost, Program.strBranchCode);
            UISetting.LoadContractTypeByBranchCode(ref cboContract_Type, Program.strBranchCode);
            UISetting.LoadDepartment(ref cboDept, Program.intBranchID);
            AddColumn();
            LoadData();


            //
            RegionClient wcf = Service.Region();
            DataTable dt;
            dt = wcf.Region_GetAll().Tables[0];
            //
            FormGlobals.TextBox_SetAutocomplete(txtEmployee_ThanhPhoKhaiSinh, dt, "Region_Name");
            FormGlobals.TextBox_SetAutocomplete(txtEmployee_ThanhPhoHoKhau, dt, "Region_Name");
            FormGlobals.TextBox_SetAutocomplete(txtEmployee_ThanhPhoLienHe, dt, "Region_Name");
            //
            FormGlobals.TextBox_SetWaterMark("Mã tỉnh", 2, txtEmployee_HospitalRegionCode);
            FormGlobals.TextBox_SetWaterMark("Mã b.viện", 2, txtMedicalPlace);
            FormGlobals.TextBox_SetWaterMark("Nếu có", 2, txtEmployee_BookNo);
            FormGlobals.TextBox_SetWaterMark("dd/MM/yyyy", 2, txtEmployee_Birthday, txtLC_Date, txtDateStart);
            FormGlobals.TextBox_SetWaterMark("MM/yyyy", 2, txtDatePC, txtDateSalary, txtDateTNN, txtDateTNVK);
            FormGlobals.TextBox_SetWaterMark("H.Số", 2, txtBasicFactor);
            FormGlobals.TextBox_SetWaterMark("Tiền", 2, txtBasicSalary);
            //
            //
            if (_EmployeeID > 0)
            {
                _employeeInfo = Employee_Controller.Instance().Employee_GetInfo(_EmployeeID);
                Info2Form();
            }

            //
            FormGlobals.Control_SetRequired(txtEmployee_FirstName, txtEmployee_LastName, txtEmployee_Birthday, txtEmployee_HospitalRegionCode, txtMedicalPlace
                , txtPD_BeginDate
                //, cboDept
                //,cboContract_Type
                , cboPost
                );


            this.FirstFocusControl = txtEmployee_BookNo;
            txtEmployee_BookNo.SetFocus_SelectAll();
        }

        public DialogResult Show_Form()
        {
            return this.ShowDialog();
        }

        //Hiển thị danh sách nhân viên chưa gọi sang bảo hiểm
        private void LoadData()
        {
            DataTable dt = Employee_Controller.Instance().Employee_GetList(_OrderBy, _OrderDirection, Program.intBranchID, currPage, pageSize, out itemCount).Tables[0];
            int n = dt.Rows.Count;
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < n; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["STT"].Value = i + 1;
                dgvList.Rows[i].Cells["Employee_ID"].Value = dt.Rows[i]["Employee_ID"].ToString();
                dgvList.Rows[i].Cells["Employee_FullName"].Value = dt.Rows[i]["Employee_FullName"].ToString();
                dgvList.Rows[i].Cells["Employee_SIBookNo"].Value = dt.Rows[i]["Employee_SIBookNo"].ToString();
                //dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(dt.Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = Globals.DB_GetValue<DateTime>(dt.Rows[i]["Employee_DateOfBirth"]);
            }
        }

        private void PageIndex()
        {
            //totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            //if (totalPage == 0)
            //    lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            //else
            //    lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void AddColumn()
        {
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "STT", "STT", "", 30, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_SIBookNo", "Số sổ BHXH", "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", "Họ tên", "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_DateOfBirth", "Ngày sinh", "dd/MM/yyyy", 80, true, true);
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        bool CheckValid() //Kiểm tra dữ liệu nhập
        {

            DateTime DateValue;

            //if (txtEmployee_LastName.Text.Trim().Length == 0)
            //{
            //    txtEmployee_LastName.Focus();
            //    MessageBox.Show("Chưa nhập họ và tên", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}

            //if (txtEmployee_FirstName.Text.Trim().Length == 0)
            //{
            //    txtEmployee_FirstName.Focus();
            //    MessageBox.Show("Chưa nhập họ và tên", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}

            if (UISetting.GetDate(txtEmployee_Birthday.Text) == DateTime.MinValue)
            {

                MessageBox.Show("Ngày sinh không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtEmployee_Birthday.SetFocus_SelectAll();
                return false;
            }

            //if (txtEmployee_IDCardNo.Text.Trim().Length == 0)
            //{
            //    txtEmployee_IDCardNo.Focus();
            //    MessageBox.Show("Chưa nhập số Chứng minh thư nhân dân", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else 
            if (txtEmployee_IDCardNo.Text.Trim().Length > 0)
                if (txtEmployee_IDCardNo.Text.Trim().Length != 9 && txtEmployee_IDCardNo.Text.Trim().Length != 12)
                {
                    txtEmployee_IDCardNo.Focus();
                    MessageBox.Show("Số Chứng minh thư nhân dân phải đủ 9 hoặc 12 ký tự", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                else
                {
                    Int64 CMTTmp = 0;
                    Int64.TryParse(txtEmployee_IDCardNo.Text.Trim(), out CMTTmp);
                    if (CMTTmp == 0)
                    {
                        MessageBox.Show("Số Chứng minh thư nhân dân phải đủ 9 hoặc 12 ký tự và đúng định dạng số", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtEmployee_IDCardNo.SetFocus_SelectAll();
                        return false;
                    }
                }

            //if (txtEmployee_HospitalRegionCode.Text.Length == 0)
            //{
            //    txtEmployee_HospitalRegionCode.Focus();
            //    MessageBox.Show("Chưa nhập nơi khám chữa bệnh", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //if (txtMedicalPlace.Text.Trim().Length == 0)
            //{
            //    txtMedicalPlace.Focus();
            //    MessageBox.Show("Chưa nhập nơi khám chữa bệnh", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}

            if (txtEmployee_HospitalRegionCode.Text.Length > 0 && txtMedicalPlace.Text.Trim().Length > 0)
                if (!_wcfHospital.Hospital_CheckExist(txtMedicalPlace.Text.Trim(), txtEmployee_HospitalRegionCode.Text))
                {

                    MessageBox.Show("Nơi khám chữa bệnh không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMedicalPlace.SetFocus_SelectAll();
                    return false;
                }

            if (txtEmployee_IDcardDate.Text.Length == 0)
                txtEmployee_IDcardDate.Text = "";
            else
            {
                if (!DateTime.TryParseExact(txtEmployee_IDcardDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
                {

                    MessageBox.Show(UpdateUI.GetString("ngaycapCMNDkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtEmployee_IDcardDate.SetFocus_SelectAll();
                    return false;
                }
            }
            if (txtEmployee_Email.Text.Trim().Length > 0)
            {
                if (!UISetting.CheckEmail(txtEmployee_Email.Text.Trim()))
                {
                    MessageBox.Show("Tài khoản email không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtEmployee_Email.SetFocus_SelectAll();
                    return false;
                }
            }
            //if (txtLC_Date.Text.Length == 0)
            //{
            //    txtLC_Date.Focus();
            //    MessageBox.Show("Chưa nhập ngày ký hợp đồng", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else
            //{
            //    if (!DateTime.TryParseExact(txtLC_Date.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            //    {
            //        txtLC_Date.Focus();
            //        MessageBox.Show("Ngày ký hợp đồng không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}
            //if (txtDateStart.Text.Length == 0)
            //{
            //    txtDateStart.Focus();
            //    MessageBox.Show("Chưa nhập ngày hiệu lực hợp đồng", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else
            //{
            //    if (!DateTime.TryParseExact(txtDateStart.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            //    {
            //        txtDateStart.Focus();
            //        MessageBox.Show("Ngày hiệu lực hợp đồng không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}
            //if (txtDateSalary.Text.Length == 0)
            //{
            //    txtDateSalary.Focus();
            //    MessageBox.Show("Chưa nhập thời điểm tăng lương chính", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else
            //{
            //    if (!DateTime.TryParseExact(txtDateSalary.Text, "MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            //    {
            //        txtDateSalary.Focus();
            //        MessageBox.Show("Thời điểm tăng lương chính không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}
            //if (txtDatePC.Text.Length == 0)
            //{
            //    txtDatePC.Focus();
            //    MessageBox.Show("Chưa nhập thời điểm tăng phụ cấp chức vụ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else
            //{
            //    if (!DateTime.TryParseExact(txtDatePC.Text, "MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            //    {
            //        txtDatePC.Focus();
            //        MessageBox.Show("Thời điểm tăng phụ cấp chức vụ không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}
            //if (txtDateTNVK.Text.Length == 0)
            //{
            //    txtDateTNVK.Focus();
            //    MessageBox.Show("Chưa nhập thời điểm tăng TN vượt khung", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else
            //{
            //    if (!DateTime.TryParseExact(txtDateTNVK.Text, "MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            //    {
            //        txtDateTNVK.Focus();
            //        MessageBox.Show("Thời điểm tăng TN vượt khung không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}
            //if (txtDateTNN.Text.Length == 0)
            //{
            //    txtDateTNN.Focus();
            //    MessageBox.Show("Chưa nhập thời điểm tăng thâm niên nghề", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else
            //{
            //    if (!DateTime.TryParseExact(txtDateTNN.Text, "MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateValue))
            //    {
            //        txtDateTNN.Focus();
            //        MessageBox.Show("Thời điểm tăng thâm niên nghề không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}

            if (txtBasicFactor.GetValue<decimal>() <= 0 && txtBasicSalary.GetValue<decimal>() <= 0)
            {
                MessageBox.Show("Mức lương hệ số hoặc tiền phải có giá trị > 0", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtBasicFactor.SetFocus_SelectAll();
                return false;
            }

            //if (Globals.Object_GetString(cboDept.SelectedValue, "") == "" || Globals.Object_GetString(cboDept.SelectedValue, "") == "0")
            //{
            //    MessageBox.Show("Chưa nhập phòng ban", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    tcHoSo.SelectedTab = tpTTChung;
            //    cboDept.Focus();
            //    return false;
            //}
            if (Globals.Object_GetString(cboPost.SelectedValue, "") == "" || Globals.Object_GetString(cboPost.SelectedValue, "") == "0")
            {
                MessageBox.Show("Chưa nhập chức vụ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tcHoSo.SelectedTab = tpBHXH;
                cboPost.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// Hiển thị danh sách quốc tịch
        /// </summary>
        private void LoadNational()
        {
            try
            {
                NationalClient wcfNational = Service.National();
                cboNational.DisplayMember = "National_Name";
                cboNational.ValueMember = "National_ID";
                cboNational.DataSource = wcfNational.National_GetAll().Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        /// <summary>
        /// Hiển thị danh sách dân tộc
        /// </summary>
        private void LoadEthnic()
        {
            try
            {
                EthnicClient wcfEthnic = Service.Ethnic();
                cboEmployee_Ethnic.DisplayMember = "Ethnic_Name";
                cboEmployee_Ethnic.ValueMember = "Ethnic_ID";
                cboEmployee_Ethnic.DataSource = wcfEthnic.Ethnic_GetAll().Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        /// <summary>
        /// Focus khỏi số sổ => lấy đơn vị đã đóng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtEmployee_BookNo_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_BookNo.Text.Trim().Length > 0 && Globals.Object_GetValue<string>(txtEmployee_BookNo.Tag) != txtEmployee_BookNo.Text.Trim())
            {
                txtEmployee_BookNo.Tag = txtEmployee_BookNo.Text.Trim();
                _employeeInfo = Employee_Controller.Instance().Employee_GetInfoBySIBookNo(txtEmployee_BookNo.Text, Program.intBranchID);
                if (_employeeInfo != null && _employeeInfo.Employee_ID > 0)
                {
                    _EmployeeID = _employeeInfo.Employee_ID;
                    _LabourInfo = LabourContract_Controller.Instance().LabourContract_GetInfo(_EmployeeID);
                    if (_LabourInfo != null)
                    {
                        _LCID = _LabourInfo.LC_ID;

                        //Nhập TM mà có số liệu từ đơn vị khác => gán LCID = 0 để insert
                        if (_employeeInfo.Employee_BranchID != Program.intBranchID)
                            _LCID = 0;

                        //
                        txtDateStart.Text = _LabourInfo.LC_BeginDate == DateTime.MinValue ? "" : _LabourInfo.LC_BeginDate.ToString("dd/MM/yyyy");
                        cboPost.SelectedValue = _LabourInfo.LC_PositionID;

                        txtLC_Date.Text = "";
                        if (_LabourInfo.LC_Date != DateTime.MinValue)
                            txtLC_Date.Text = _LabourInfo.LC_Date.ToString("dd/MM/yyyy");

                        //txtLC_Date.Text = _LabourInfo.LC_Date.ToString("dd/MM/yyyy");
                        txtLC_No.Text = _LabourInfo.LC_No;
                        cboContract_Type.SelectedValue = _LabourInfo.LC_ContractTypeID;
                        cboDept.SelectedValue = _LabourInfo.LC_DepartmentID;
                    }
                    _PDInfo = PositionDetail_Controller.Instance().PositionDetail_GetInfo(_EmployeeID);
                    if (_PDInfo != null)
                    {
                        _PDID = _PDInfo.PD_ID;

                        //
                        if (_employeeInfo.Employee_BranchID != Program.intBranchID)
                            _PDID = 0;

                        //
                        if (_PDInfo.PD_DepartmentID > 0)
                            cboDept.SelectedValue = _PDInfo.PD_DepartmentID;
                        if (_PDInfo.PD_PositionID > 0)
                            cboPost.SelectedValue = _PDInfo.PD_PositionID;

                        txtDateSalary.Text = _PDInfo.PD_DateSalary == DateTime.MinValue ? "" : _PDInfo.PD_DateSalary.ToString("MM/yyyy");
                        txtDatePC.Text = _PDInfo.PD_DatePC == DateTime.MinValue ? "" : _PDInfo.PD_DatePC.ToString("MM/yyyy");
                        txtDateTNVK.Text = _PDInfo.PD_DateTNVK == DateTime.MinValue ? "" : _PDInfo.PD_DateTNVK.ToString("MM/yyyy");
                        txtDateTNN.Text = _PDInfo.PD_DateTNN == DateTime.MinValue ? "" : _PDInfo.PD_DateTNN.ToString("MM/yyyy");
                        txtBasicSalary.Text = _PDInfo.PD_BasicSalary.ToString(("#,##0"));
                        txtBasicFactor.Text = _PDInfo.PD_BasicFactor.ToString(UIParamater.Cham2So);

                        txtPositionFactor.Text = _PDInfo.PD_PositionFactor.ToString(UIParamater.Cham3So);
                        txtPositionAllowance.Text = _PDInfo.PD_PositionAllowance.ToString("#,##0");

                        txtOverFactor.Text = _PDInfo.PD_OverFactor.ToString(UIParamater.Cham3So);
                        txtOverAllowance.Text = _PDInfo.PD_OverAllowance.ToString("#,##0");

                        txtJobFactor.Text = _PDInfo.PD_JobFactor.ToString(UIParamater.Cham3So);
                        txtJobAllowance.Text = _PDInfo.PD_JobAllowance.ToString("#,##0");
                        txtOther.Text = _PDInfo.PD_OtherAllowance.ToString("#,##0");

                        if (_PDInfo.PD_BasicSalary != 0)
                        {
                            txtOverFactor_Percent.Text = (_PDInfo.PD_OverAllowance * 100 / _PDInfo.PD_BasicSalary).ToString(UIParamater.Cham3So);
                            txtJobFactor_Percent.Text = (_PDInfo.PD_JobAllowance * 100 / _PDInfo.PD_BasicSalary).ToString(UIParamater.Cham3So);
                            txtJobFactor.Visible = txtOverFactor.Visible = false;
                            txtJobAllowance.Visible = txtOverAllowance.Visible = true;
                        }
                        else if (_PDInfo.PD_BasicFactor != 0)
                        {
                            txtOverFactor_Percent.Text = (_PDInfo.PD_OverFactor * 100 / _PDInfo.PD_BasicFactor).ToString(UIParamater.Cham3So);
                            txtJobFactor_Percent.Text = (_PDInfo.PD_JobFactor * 100 / _PDInfo.PD_BasicFactor).ToString(UIParamater.Cham3So);
                            txtJobAllowance.Visible = txtOverAllowance.Visible = false;
                            txtJobFactor.Visible = txtOverFactor.Visible = true;
                        }
                        else if (_PDInfo.PD_BasicFactor == 0 && _PDInfo.PD_BasicSalary == 0)
                            txtOverFactor_Percent.Text = txtJobFactor_Percent.Text = "0.00";
                    }

                    if (_employeeInfo.Employee_BranchID > 0)
                        txtBranchName.Text = _wcfBranch.Branch_GetInfo(_employeeInfo.Employee_BranchID).Name;
                    txtEmployee_LastName.Text = _employeeInfo.Employee_LastName;
                    txtEmployee_FirstName.Text = _employeeInfo.Employee_FirstName;
                    if (_employeeInfo.Employee_AddressLH != "" && _employeeInfo.Employee_AddressLH != null)
                    {
                        string[] AddressLH = _employeeInfo.Employee_AddressLH.Split(new char[] { '|' });
                        if (AddressLH.Count() == 4)
                        {
                            txtEmployee_DiaChiLienHe.Text = AddressLH[0] == null ? "" : AddressLH[0];
                            txtEmployee_PhuongLienHe.Text = AddressLH[1] == null ? "" : AddressLH[1];
                            txtEmployee_QuanLienHe.Text = AddressLH[2] == null ? "" : AddressLH[2];
                            txtEmployee_ThanhPhoLienHe.Text = AddressLH[3] == null ? "" : AddressLH[3];
                        }
                        else
                            txtEmployee_DiaChiLienHe.Text = _employeeInfo.Employee_AddressLH;
                    }
                    else
                    {
                        txtEmployee_DiaChiLienHe.Text = "";
                        txtEmployee_PhuongLienHe.Text = "";
                        txtEmployee_QuanLienHe.Text = "";
                        txtEmployee_ThanhPhoLienHe.Text = "";
                    }
                    if (_employeeInfo.Employee_AddressHK != "" && _employeeInfo.Employee_AddressHK != null)
                    {
                        string[] AddressHK = _employeeInfo.Employee_AddressHK.Split(new char[] { '|' });
                        if (AddressHK.Count() == 4)
                        {
                            txtEmployee_DiaChiHoKhau.Text = AddressHK[0] == null ? "" : AddressHK[0];
                            txtEmployee_PhuongHoKhau.Text = AddressHK[1] == null ? "" : AddressHK[1];
                            txtEmployee_QuanHoKhau.Text = AddressHK[2] == null ? "" : AddressHK[2];
                            txtEmployee_ThanhPhoHoKhau.Text = AddressHK[3] == null ? "" : AddressHK[3];
                        }
                        else
                            txtEmployee_DiaChiHoKhau.Text = _employeeInfo.Employee_AddressHK;
                    }
                    else
                    {
                        txtEmployee_DiaChiHoKhau.Text = "";
                        txtEmployee_PhuongHoKhau.Text = "";
                        txtEmployee_QuanHoKhau.Text = "";
                        txtEmployee_ThanhPhoHoKhau.Text = "";
                    }
                    if (_employeeInfo.Employee_AddressKS != "" && _employeeInfo.Employee_AddressKS != null)
                    {
                        string[] AddressKS = _employeeInfo.Employee_AddressKS.Split(new char[] { '|' });
                        if (AddressKS.Count() == 4)
                        {
                            txtEmployee_DiaChiKhaiSinh.Text = AddressKS[0] == null ? "" : AddressKS[0];
                            txtEmployee_PhuongKhaiSinh.Text = AddressKS[1] == null ? "" : AddressKS[1];
                            txtEmployee_QuanKhaiSinh.Text = AddressKS[2] == null ? "" : AddressKS[2];
                            txtEmployee_ThanhPhoKhaiSinh.Text = AddressKS[3] == null ? "" : AddressKS[3];
                        }
                        else
                            txtEmployee_DiaChiKhaiSinh.Text = _employeeInfo.Employee_AddressKS;
                    }
                    else
                    {
                        txtEmployee_DiaChiKhaiSinh.Text = "";
                        txtEmployee_PhuongKhaiSinh.Text = "";
                        txtEmployee_QuanKhaiSinh.Text = "";
                        txtEmployee_ThanhPhoKhaiSinh.Text = "";
                    }
                    if (_employeeInfo.Employee_DateOfBirth != DateTime.MinValue)
                        txtEmployee_Birthday.Text = _employeeInfo.Employee_DateOfBirth.ToString("dd/MM/yyyy");
                    txtEmployee_Phone.Text = _employeeInfo.Employee_Phone;
                    txtEmployee_Email.Text = _employeeInfo.Employee_Email;
                    cboNational.SelectedValue = _employeeInfo.Employee_Nationality;
                    txtMedicalPlace.Text = _employeeInfo.Employee_MedicalPlace;
                    txtEmployee_MedicalNo.Text = _employeeInfo.Employee_MedicalNo;
                    //
                    if (String.IsNullOrEmpty(_employeeInfo.Employee_MedicalRegionCode))
                        _employeeInfo.Employee_MedicalRegionCode = Program.strRegionCode;
                    txtEmployee_HospitalRegionCode.Text = _employeeInfo.Employee_MedicalRegionCode;

                    _HospitalInfo = new HospitalInfo();
                    _HospitalInfo = _wcfHospital.Hospital_GetInfoByCode(!String.IsNullOrEmpty(_employeeInfo.Employee_MedicalPlace) ? _employeeInfo.Employee_MedicalPlace : "", _employeeInfo.Employee_MedicalRegionCode);
                    if (_HospitalInfo != null)
                        txtHospitalName.Text = _HospitalInfo.Name;
                    txtEmployee_IDCardNo.Text = _employeeInfo.Employee_IDCardNo;
                    txtEmployee_IDcardDate.Text = _employeeInfo.Employee_IDCardIssueDate == DateTime.MinValue ? "" : _employeeInfo.Employee_IDCardIssueDate.ToString("dd/MM/yyyy");
                    txtEmployee_IDCardPlace.Text = _employeeInfo.Employee_IDCardIssueAuthority;
                    txtEmployee_BookNo.Text = _employeeInfo.Employee_SIBookNo;
                    if (_wcfDetail.Detail_CheckExistSIBookNo(_employeeInfo.Employee_SIBookNo, Program.strBranchCode))
                    {
                        txtEmployee_BookNo.ReadOnly = true;
                        txtEmployee_BookNo.BackColor = Color.White;
                    }
                    cboEmployee_Ethnic.SelectedValue = _employeeInfo.Employee_EthlicID;
                    txtEmployee_ChaMe.Text = _employeeInfo.Employee_ChaMe;
                    txtEmployee_ThanNhanKhac.Text = _employeeInfo.Employee_ThanNhanKhac;
                    if (_employeeInfo.Employee_Sex)
                        radMale.Checked = _employeeInfo.Employee_Sex;
                    else
                        radFemale.Checked = true;
                    chkResign.Checked = _employeeInfo.Employee_IsQuited;
                    cboUnJobFactor.Checked = _employeeInfo.Employee_UnJobFactor;
                    cboUnOverFactor.Checked = _employeeInfo.Employee_UnOverFactor;
                    //
                    txtEmployee_ThuNhapBHTN.Text = "0";
                    if (_employeeInfo.Employee_ThuNhapBHTN > 0)
                        txtEmployee_ThuNhapBHTN.Text = _employeeInfo.Employee_ThuNhapBHTN.ToString("#,##0");


                    txtEmployee_PhuongThucBHTN.Text = _employeeInfo.Employee_PhuongThucBHTN;
                    txtEmployee_DoiTuongBHYT.Text = _employeeInfo.Employee_DoiTuongBHYT;
                    txtEmployee_MucTienBHYT.Text = "0";
                    if (_employeeInfo.Employee_MucTienBHYT > 0)
                        txtEmployee_MucTienBHYT.Text = _employeeInfo.Employee_MucTienBHYT.ToString("#,##0");
                    txtEmployee_PhuongThucBHYT.Text = _employeeInfo.Employee_PhuongThucBHYT;


                }
                else
                {
                    //FormGlobals.Message_Warning("Chưa tồn tại số sổ này");
                    //txtEmployee_BookNo.SetFocus_SelectAll();

                }
            }
        }


        /// <summary>
        /// Hiển thị dữ liệu từ object lên form
        /// </summary>
        private void Info2Form()
        {
            try
            {
                _EmployeeID = _employeeInfo.Employee_ID;
                _LabourInfo = LabourContract_Controller.Instance().LabourContract_GetInfo(_EmployeeID);
                if (_LabourInfo != null)
                {
                    _LCID = _LabourInfo.LC_ID;
                    txtDateStart.Text = _LabourInfo.LC_BeginDate == DateTime.MinValue ? "" : _LabourInfo.LC_BeginDate.ToString("dd/MM/yyyy");
                    //cboPost.SelectedValue = _LabourInfo.LC_PositionID;
                    txtLC_Date.Text = "";
                    if (_LabourInfo.LC_Date != DateTime.MinValue)
                        txtLC_Date.Text = _LabourInfo.LC_Date.ToString("dd/MM/yyyy");
                    txtLC_No.Text = _LabourInfo.LC_No;
                    cboContract_Type.SelectedValue = _LabourInfo.LC_ContractTypeID;
                    //cboDept.SelectedValue = _LabourInfo.LC_DepartmentID;
                }
                _PDInfo = PositionDetail_Controller.Instance().PositionDetail_GetInfo(_EmployeeID);
                if (_PDInfo != null)
                {
                    //Chuc vu va phong ban, lay theo Position Detail, chu ko lay theo hop dong ban dau
                    _PDID = _PDInfo.PD_ID;
                    if (_PDInfo.PD_DepartmentID > 0)
                        cboDept.SelectedValue = _PDInfo.PD_DepartmentID;
                    if (_PDInfo.PD_PositionID > 0)
                        cboPost.SelectedValue = _PDInfo.PD_PositionID;
                    //
                    txtDateSalary.Text = _PDInfo.PD_DateSalary == DateTime.MinValue ? "" : _PDInfo.PD_DateSalary.ToString("MM/yyyy");
                    txtDatePC.Text = _PDInfo.PD_DatePC == DateTime.MinValue ? "" : _PDInfo.PD_DatePC.ToString("MM/yyyy");
                    txtDateTNVK.Text = _PDInfo.PD_DateTNVK == DateTime.MinValue ? "" : _PDInfo.PD_DateTNVK.ToString("MM/yyyy");
                    txtDateTNN.Text = _PDInfo.PD_DateTNN == DateTime.MinValue ? "" : _PDInfo.PD_DateTNN.ToString("MM/yyyy");


                    txtPD_BeginDate.Text = _PDInfo.PD_BeginDate == DateTime.MinValue ? "" : _PDInfo.PD_BeginDate.ToString("MM/yyyy");
                    txtBasicSalary.Text = _PDInfo.PD_BasicSalary.ToString(("#,##0.00"));
                    txtBasicFactor.Text = _PDInfo.PD_BasicFactor.ToString(UIParamater.Cham2So);

                    txtPositionFactor.Text = _PDInfo.PD_PositionFactor.ToString(UIParamater.Cham3So);
                    txtPositionAllowance.Text = _PDInfo.PD_PositionAllowance.ToString("#,##0.00");

                    txtOverFactor_Percent.Text = Globals.DB_GetNull(_PDInfo.PD_OverFactorPercent, "").ToString();
                    txtOverFactor.Text = _PDInfo.PD_OverFactor.ToString(UIParamater.Cham3So);
                    txtOverAllowance.Text = _PDInfo.PD_OverAllowance.ToString("#,##0.00");

                    txtJobFactor_Percent.Text = Globals.DB_GetNull(_PDInfo.PD_JobFactorPercent, "").ToString();
                    txtJobFactor.Text = _PDInfo.PD_JobFactor.ToString(UIParamater.Cham3So);
                    txtJobAllowance.Text = _PDInfo.PD_JobAllowance.ToString("#,##0.00");

                    //if (_PDInfo.PD_BasicSalary != 0)
                    //{
                    //    txtOverFactor_Percent.Text = (_PDInfo.PD_OverAllowance * 100 / _PDInfo.PD_BasicSalary).ToString(UIParamater.Cham3So);
                    //    txtJobFactor_Percent.Text = (_PDInfo.PD_JobAllowance * 100 / _PDInfo.PD_BasicSalary).ToString(UIParamater.Cham3So);
                    //    txtJobFactor.Visible = txtOverFactor.Visible = false;
                    //    txtJobAllowance.Visible = txtOverAllowance.Visible = true;
                    //}
                    //else if (_PDInfo.PD_BasicFactor != 0)
                    //{
                    //    txtOverFactor_Percent.Text = (_PDInfo.PD_OverFactor * 100 / _PDInfo.PD_BasicFactor).ToString(UIParamater.Cham3So);
                    //    txtJobFactor_Percent.Text = (_PDInfo.PD_JobFactor * 100 / _PDInfo.PD_BasicFactor).ToString(UIParamater.Cham3So);
                    //    txtJobAllowance.Visible = txtOverAllowance.Visible = false;
                    //    txtJobFactor.Visible = txtOverFactor.Visible = true;
                    //}
                    //else if (_PDInfo.PD_BasicFactor == 0 && _PDInfo.PD_BasicSalary == 0)
                    //    txtOverFactor_Percent.Text = txtJobFactor_Percent.Text = "0.00";

                    txtOther.Text = _PDInfo.PD_OtherAllowance.ToString("#,##0.00");
                }
                if (_employeeInfo.Employee_BranchID > 0)
                    txtBranchName.Text = _wcfBranch.Branch_GetInfo(_employeeInfo.Employee_BranchID).Name;
                txtEmployee_FirstName.Text = _employeeInfo.Employee_FirstName;
                txtEmployee_LastName.Text = _employeeInfo.Employee_LastName;
                if (_employeeInfo.Employee_AddressLH != "" && _employeeInfo.Employee_AddressLH != null)
                {
                    string[] AddressLH = _employeeInfo.Employee_AddressLH.Split(new char[] { '|' });
                    if (AddressLH.Count() == 4)
                    {
                        txtEmployee_DiaChiLienHe.Text = AddressLH[0] == null ? "" : AddressLH[0];
                        txtEmployee_PhuongLienHe.Text = AddressLH[1] == null ? "" : AddressLH[1];
                        txtEmployee_QuanLienHe.Text = AddressLH[2] == null ? "" : AddressLH[2];
                        txtEmployee_ThanhPhoLienHe.Text = AddressLH[3] == null ? "" : AddressLH[3];
                    }
                    else
                        txtEmployee_DiaChiLienHe.Text = _employeeInfo.Employee_AddressLH;
                }
                else
                {
                    txtEmployee_DiaChiLienHe.Text = "";
                    txtEmployee_PhuongLienHe.Text = "";
                    txtEmployee_QuanLienHe.Text = "";
                    txtEmployee_ThanhPhoLienHe.Text = "";
                }
                if (_employeeInfo.Employee_AddressHK != "" && _employeeInfo.Employee_AddressHK != null)
                {
                    string[] AddressHK = _employeeInfo.Employee_AddressHK.Split(new char[] { '|' });
                    if (AddressHK.Count() == 4)
                    {
                        txtEmployee_DiaChiHoKhau.Text = AddressHK[0] == null ? "" : AddressHK[0];
                        txtEmployee_PhuongHoKhau.Text = AddressHK[1] == null ? "" : AddressHK[1];
                        txtEmployee_QuanHoKhau.Text = AddressHK[2] == null ? "" : AddressHK[2];
                        txtEmployee_ThanhPhoHoKhau.Text = AddressHK[3] == null ? "" : AddressHK[3];
                    }
                    else
                        txtEmployee_DiaChiHoKhau.Text = _employeeInfo.Employee_AddressHK;
                }
                else
                {
                    txtEmployee_DiaChiHoKhau.Text = "";
                    txtEmployee_PhuongHoKhau.Text = "";
                    txtEmployee_QuanHoKhau.Text = "";
                    txtEmployee_ThanhPhoHoKhau.Text = "";
                }
                if (_employeeInfo.Employee_AddressKS != "" && _employeeInfo.Employee_AddressKS != null)
                {
                    string[] AddressKS = _employeeInfo.Employee_AddressKS.Split(new char[] { '|' });
                    if (AddressKS.Count() == 4)
                    {
                        txtEmployee_DiaChiKhaiSinh.Text = AddressKS[0] == null ? "" : AddressKS[0];
                        txtEmployee_PhuongKhaiSinh.Text = AddressKS[1] == null ? "" : AddressKS[1];
                        txtEmployee_QuanKhaiSinh.Text = AddressKS[2] == null ? "" : AddressKS[2];
                        txtEmployee_ThanhPhoKhaiSinh.Text = AddressKS[3] == null ? "" : AddressKS[3];
                    }
                    else
                        txtEmployee_DiaChiKhaiSinh.Text = _employeeInfo.Employee_AddressKS;
                }
                else
                {
                    txtEmployee_DiaChiKhaiSinh.Text = "";
                    txtEmployee_PhuongKhaiSinh.Text = "";
                    txtEmployee_QuanKhaiSinh.Text = "";
                    txtEmployee_ThanhPhoKhaiSinh.Text = "";
                }
                if (_employeeInfo.Employee_DateOfBirth != DateTime.MinValue)
                    txtEmployee_Birthday.Text = _employeeInfo.Employee_DateOfBirth.ToString("dd/MM/yyyy");
                txtEmployee_Phone.Text = _employeeInfo.Employee_Phone;
                txtEmployee_Email.Text = _employeeInfo.Employee_Email;
                cboNational.SelectedValue = _employeeInfo.Employee_Nationality;
                txtMedicalPlace.Text = _employeeInfo.Employee_MedicalPlace;
                txtEmployee_MedicalNo.Text = _employeeInfo.Employee_MedicalNo;
                txtEmployee_Code.Text = _employeeInfo.Employee_Code;
                //
                if (String.IsNullOrEmpty(_employeeInfo.Employee_MedicalRegionCode))
                    _employeeInfo.Employee_MedicalRegionCode = Program.strRegionCode;

                txtEmployee_HospitalRegionCode.Text = _employeeInfo.Employee_MedicalRegionCode;


                _HospitalInfo = new HospitalInfo();
                _HospitalInfo = _wcfHospital.Hospital_GetInfoByCode(_employeeInfo.Employee_MedicalPlace, _employeeInfo.Employee_MedicalRegionCode);
                if (_HospitalInfo != null)
                    txtHospitalName.Text = _HospitalInfo.Name;
                txtEmployee_IDCardNo.Text = _employeeInfo.Employee_IDCardNo;
                txtEmployee_IDcardDate.Text = _employeeInfo.Employee_IDCardIssueDate == DateTime.MinValue ? "" : _employeeInfo.Employee_IDCardIssueDate.ToString("dd/MM/yyyy");
                txtEmployee_IDCardPlace.Text = _employeeInfo.Employee_IDCardIssueAuthority;
                txtEmployee_BookNo.Text = _employeeInfo.Employee_SIBookNo;
                txtEmployee_BookNo.Tag = _employeeInfo.Employee_SIBookNo;
                if (_wcfDetail.Detail_CheckExistSIBookNo(_employeeInfo.Employee_SIBookNo, Program.strBranchCode))
                {
                    txtEmployee_BookNo.ReadOnly = true;
                    txtEmployee_BookNo.BackColor = Color.White;
                }

                cboEmployee_Ethnic.SelectedValue = _employeeInfo.Employee_EthlicID;
                txtEmployee_ChaMe.Text = _employeeInfo.Employee_ChaMe;
                txtEmployee_ThanNhanKhac.Text = _employeeInfo.Employee_ThanNhanKhac;
                if (_employeeInfo.Employee_Sex)
                    radMale.Checked = _employeeInfo.Employee_Sex;
                else
                    radFemale.Checked = true;
                chkResign.Checked = _employeeInfo.Employee_IsQuited;
                cboUnJobFactor.Checked = _employeeInfo.Employee_UnJobFactor;
                cboUnOverFactor.Checked = _employeeInfo.Employee_UnOverFactor;
                //
                txtEmployee_ThuNhapBHTN.Text = "0";
                if (_employeeInfo.Employee_ThuNhapBHTN > 0)
                    txtEmployee_ThuNhapBHTN.Text = _employeeInfo.Employee_ThuNhapBHTN.ToString("#,##0");


                txtEmployee_PhuongThucBHTN.Text = _employeeInfo.Employee_PhuongThucBHTN;
                txtEmployee_DoiTuongBHYT.Text = _employeeInfo.Employee_DoiTuongBHYT;
                txtEmployee_MucTienBHYT.Text = "0";
                if (_employeeInfo.Employee_MucTienBHYT > 0)
                    txtEmployee_MucTienBHYT.Text = _employeeInfo.Employee_MucTienBHYT.ToString("#,##0");
                txtEmployee_PhuongThucBHYT.Text = _employeeInfo.Employee_PhuongThucBHYT;


                //
                txtEmployee_BookNo.SetFocus_SelectAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        /// <summary>
        /// Lưu dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!FormGlobals.Panel_CheckError(tpTTChung))
                return;
            //
            if (!CheckValid())
            {
                return;
            }
            bool retValLC = true;
            bool retValPD = true;
            bool retVal = true;
            Form2Info();
            if (txtEmployee_BookNo.Text.Trim().Length > 0)
            {
                string BranchName = "";
                BranchName = _wcfBranch.Branch_GetInfo(_employeeInfo.Employee_BranchID).Name;
                if (BranchName != txtBranchName.Text)
                {
                    _employeeInfo.Employee_ID = Employee_Controller.Instance().Employee_InsertNS(_employeeInfo);
                    _EmployeeID = _employeeInfo.Employee_ID;
                    if (_EmployeeID > 0)
                    {
                        FormLC2Info();
                        FormPD2Info();
                        if (_PDID > 0)
                        {
                            if (PositionDetail_Controller.Instance().PositionDetail_UpdateNS(_PDInfo))
                                retValPD = true;
                            else
                                retValPD = false;
                        }
                        else
                        {
                            _PDInfo.PD_ID = PositionDetail_Controller.Instance().PositionDetail_InsertNS(_PDInfo);
                            _PDID = _PDInfo.PD_ID;
                            if (_PDID > 0)
                            {
                                retValPD = true;
                            }
                            else
                                retValPD = false;
                        }
                        if (_LCID > 0)
                        {
                            if (LabourContract_Controller.Instance().LabourContract_UpdateNS(_LabourInfo))
                                retValLC = true;
                            else
                                retValLC = false;
                        }
                        else
                        {
                            _LabourInfo.LC_ID = LabourContract_Controller.Instance().LabourContract_InsertNS(_LabourInfo);
                            _LCID = _LabourInfo.LC_ID;
                            if (_LCID > 0)
                            {
                                retValLC = true;
                            }
                            else
                                retValLC = false;
                        }
                        retVal = true;
                    }
                    else
                    {
                        retVal = false;
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    if (Employee_Controller.Instance().Employee_UpdateNS(_employeeInfo))
                    {
                        FormLC2Info();
                        if (_LCID > 0)
                        {
                            if (LabourContract_Controller.Instance().LabourContract_UpdateNS(_LabourInfo))
                                retValLC = true;
                            else
                                retValLC = false;
                        }
                        else
                        {
                            _LabourInfo.LC_ID = LabourContract_Controller.Instance().LabourContract_InsertNS(_LabourInfo);
                            _LCID = _LabourInfo.LC_ID;
                            if (_LCID > 0)
                            {
                                retValLC = true;
                            }
                            else
                                retValLC = false;
                        }
                        FormPD2Info();
                        if (_PDID > 0)
                        {
                            if (PositionDetail_Controller.Instance().PositionDetail_UpdateNS(_PDInfo))
                                retValPD = true;
                            else
                                retValPD = false;
                        }
                        else
                        {
                            _PDInfo.PD_ID = PositionDetail_Controller.Instance().PositionDetail_InsertNS(_PDInfo);
                            _PDID = _PDInfo.PD_ID;
                            if (_PDID > 0)
                                retValPD = true;
                            else
                                retValPD = false;
                        }
                        retVal = true;
                    }
                    else
                    {
                        retVal = false;
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                }
            }
            else
            {
                _employeeInfo.Employee_ID = Employee_Controller.Instance().Employee_InsertNS(_employeeInfo);
                _EmployeeID = _employeeInfo.Employee_ID;
                txtEmployee_BookNo.Text = _employeeInfo.Employee_SIBookNo;
                if (_EmployeeID > 0)
                {
                    retVal = true;
                    FormLC2Info();
                    FormPD2Info();
                    if (_PDID > 0)
                    {
                        if (PositionDetail_Controller.Instance().PositionDetail_UpdateNS(_PDInfo))
                            retValPD = true;
                        else
                            retValPD = false;
                    }
                    else
                    {
                        _PDInfo.PD_ID = PositionDetail_Controller.Instance().PositionDetail_InsertNS(_PDInfo);
                        _PDID = _PDInfo.PD_ID;
                        if (_PDID > 0)
                        {
                            retValPD = true;
                        }
                        else
                            retValPD = false;
                    }
                    if (_LCID > 0)
                    {
                        if (LabourContract_Controller.Instance().LabourContract_UpdateNS(_LabourInfo))
                            retValLC = true;
                        else
                            retValLC = false;
                    }
                    else
                    {
                        _LabourInfo.LC_ID = LabourContract_Controller.Instance().LabourContract_InsertNS(_LabourInfo);
                        _LCID = _LabourInfo.LC_ID;
                        if (_LCID > 0)
                        {
                            retValLC = true;
                        }
                        else
                            retValLC = false;
                    }
                }
                else
                {
                    retVal = false;
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (retValLC)
            {
                if (retValPD)

                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                else
                    MessageBox.Show("Cập nhật mức lương không thành công", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                MessageBox.Show("Cập nhật phần hợp đồng không thành công", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            LoadData();

            if (retValLC && retValPD)
            {
                FormGlobals.Panel_ClearData(tpTTChung, tpBHXH);
                tcHoSo.SelectedTab = tpTTChung;
                txtEmployee_BookNo.SetFocus_SelectAll();
            }
        }

        private void FormPD2Info()
        {
            try
            {
                _PDInfo.PD_ID = _PDID;
                _PDInfo.PD_EmployeeID = _EmployeeID;
                _PDInfo.PD_PositionID = Convert.ToInt32(cboPost.SelectedValue);
                _PDInfo.PD_DepartmentID = Globals.Object_GetValue<int>(cboDept.SelectedValue);
                _PDInfo.PD_DateSalary = txtDateSalary.GetValue<DateTime>();
                _PDInfo.PD_BeginDate = txtPD_BeginDate.GetValue<DateTime>();
                _PDInfo.PD_DatePC = txtDatePC.GetValue<DateTime>();
                _PDInfo.PD_DateTNN = txtDateTNN.GetValue<DateTime>();
                _PDInfo.PD_DateTNVK = txtDateTNVK.GetValue<DateTime>();
                _PDInfo.PD_BasicFactor = txtBasicFactor.GetValue<decimal>();

                _PDInfo.PD_BasicSalary = txtBasicSalary.GetValue<decimal>();

                _PDInfo.PD_PositionFactor = txtPositionFactor.GetValue<decimal>();
                _PDInfo.PD_PositionAllowance = txtPositionAllowance.GetValue<decimal>();

                _PDInfo.PD_OverFactorPercent = txtOverFactor_Percent.GetValue<int>();
                _PDInfo.PD_OverFactor = txtOverFactor.GetValue<decimal>();

                _PDInfo.PD_OverAllowance = txtOverAllowance.GetValue<decimal>();
                _PDInfo.PD_OtherAllowance = txtOther.GetValue<decimal>();
                //
                _PDInfo.PD_JobFactorPercent = txtJobFactor_Percent.GetValue<int>();
                _PDInfo.PD_JobFactor = txtJobFactor.GetValue<decimal>();
                _PDInfo.PD_JobAllowance = txtJobAllowance.GetValue<decimal>();
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void FormLC2Info()
        {
            try
            {
                _LabourInfo.LC_EmployeeID = _EmployeeID;
                _LabourInfo.LC_ID = _LCID;
                _LabourInfo.LC_No = txtLC_No.Text;
                _LabourInfo.LC_Date = txtLC_Date.GetValue<DateTime>();
                _LabourInfo.LC_BeginDate = txtDateStart.GetValue<DateTime>();
                _LabourInfo.LC_ContractTypeID = Globals.Object_GetValue<int>(cboContract_Type.SelectedValue);
                _LabourInfo.LC_PositionID = Globals.Object_GetValue<int>(cboPost.SelectedValue);
                _LabourInfo.LC_DepartmentID = Globals.Object_GetValue<int>(cboDept.SelectedValue);
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        /// <summary>
        /// Lấy dữ liệu từ form gán vào object
        /// </summary>
        private void Form2Info()
        {
            try
            {
                _employeeInfo.Employee_ID = _EmployeeID;
                _employeeInfo.Employee_BranchID = Program.intBranchID;
                _employeeInfo.Employee_FirstName = txtEmployee_FirstName.Text;
                _employeeInfo.Employee_LastName = txtEmployee_LastName.Text;
                _employeeInfo.Employee_DateOfBirth = UISetting.GetDate(txtEmployee_Birthday.Text);
                _employeeInfo.Employee_AddressLH = txtEmployee_DiaChiLienHe.Text + "|" + txtEmployee_PhuongLienHe.Text + "|" + txtEmployee_QuanLienHe.Text + "|" + txtEmployee_ThanhPhoLienHe.Text;
                _employeeInfo.Employee_AddressHK = txtEmployee_DiaChiHoKhau.Text + "|" + txtEmployee_PhuongHoKhau.Text + "|" + txtEmployee_QuanHoKhau.Text + "|" + txtEmployee_ThanhPhoHoKhau.Text;
                _employeeInfo.Employee_AddressKS = txtEmployee_DiaChiKhaiSinh.Text + "|" + txtEmployee_PhuongKhaiSinh.Text + "|" + txtEmployee_QuanKhaiSinh.Text + "|" + txtEmployee_ThanhPhoKhaiSinh.Text;
                _employeeInfo.Employee_Phone = txtEmployee_Phone.Text;
                _employeeInfo.Employee_Email = txtEmployee_Email.Text;
                _employeeInfo.Employee_EthlicID = Convert.ToInt32(cboEmployee_Ethnic.SelectedValue);
                _employeeInfo.Employee_Nationality = Convert.ToInt32(cboNational.SelectedValue);
                _employeeInfo.Employee_MedicalPlace = txtMedicalPlace.Text;
                _employeeInfo.Employee_MedicalRegionCode = txtEmployee_HospitalRegionCode.Text;
                _employeeInfo.Employee_MedicalNo = txtEmployee_MedicalNo.Text;
                _employeeInfo.Employee_IDCardNo = txtEmployee_IDCardNo.Text;
                _employeeInfo.Employee_IDCardIssueDate = UISetting.GetDate(txtEmployee_IDcardDate.Text);
                _employeeInfo.Employee_IDCardIssueAuthority = txtEmployee_IDCardPlace.Text;

                _employeeInfo.Employee_SIBookNo = txtEmployee_BookNo.Text;
                _employeeInfo.Employee_Sex = radMale.Checked;

                _employeeInfo.Employee_ChaMe = txtEmployee_ChaMe.Text;
                _employeeInfo.Employee_ThanNhanKhac = txtEmployee_ThanNhanKhac.Text;

                _employeeInfo.Employee_IsQuited = chkResign.Checked;
                _employeeInfo.Employee_BranchID = Program.intBranchID;
                //hiepdh: Tinh TNN, TNVK hay ko
                _employeeInfo.Employee_UnOverFactor = cboUnOverFactor.Checked;
                _employeeInfo.Employee_UnJobFactor = cboUnJobFactor.Checked;
                _employeeInfo.Employee_ThuNhapBHTN = txtEmployee_ThuNhapBHTN.GetValue<decimal>();
                _employeeInfo.Employee_PhuongThucBHTN = txtEmployee_PhuongThucBHTN.Text;
                _employeeInfo.Employee_DoiTuongBHYT = txtEmployee_DoiTuongBHYT.Text;
                _employeeInfo.Employee_MucTienBHYT = txtEmployee_MucTienBHYT.GetValue<decimal>();
                _employeeInfo.Employee_PhuongThucBHYT = txtEmployee_PhuongThucBHYT.Text;

            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        /// <summary>
        /// Bấm vào ô danh sách nhân sự, load thông tin nhân sự đó
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (Convert.ToInt32(dgvList.CurrentRow.Cells["Employee_ID"].Value) > 0)
                {
                    _EmployeeID = Convert.ToInt32(dgvList.CurrentRow.Cells["Employee_ID"].Value);
                    _employeeInfo = Employee_Controller.Instance().Employee_GetInfo(_EmployeeID);
                    Info2Form();
                }
            }
        }

        private void btnBuoc2_Click(object sender, EventArgs e)
        {
            tcHoSo.SelectedTab = tpBHXH;
        }

        private void btnBuoc1_Click(object sender, EventArgs e)
        {
            tcHoSo.SelectedTab = tpTTChung;
        }

        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            string Type = "InToKhaiBHXH_TK1_TS";
            frmPreviewReport f = new frmPreviewReport(_EmployeeID, Type);
            f.Show(this);
        }

        /// <summary>
        /// Tự động fill địa chỉ tương ứng cho 3 loại
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtDiaChiKhaiSinh_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_DiaChiHoKhau.Text == "")
                txtEmployee_DiaChiHoKhau.Text = txtEmployee_DiaChiKhaiSinh.Text;
            if (txtEmployee_DiaChiLienHe.Text == "")
                txtEmployee_DiaChiLienHe.Text = txtEmployee_DiaChiKhaiSinh.Text;
        }

        private void txtPhuongKhaiSinh_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_PhuongHoKhau.Text == "")
                txtEmployee_PhuongHoKhau.Text = txtEmployee_PhuongKhaiSinh.Text;
            if (txtEmployee_PhuongLienHe.Text == "")
                txtEmployee_PhuongLienHe.Text = txtEmployee_PhuongKhaiSinh.Text;
        }

        private void txtQuanKhaiSinh_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_QuanHoKhau.Text == "")
                txtEmployee_QuanHoKhau.Text = txtEmployee_QuanKhaiSinh.Text;
            if (txtEmployee_QuanLienHe.Text == "")
                txtEmployee_QuanLienHe.Text = txtEmployee_QuanKhaiSinh.Text;
        }

        private void txtThanhPhoKhaiSinh_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_ThanhPhoHoKhau.Text == "")
                txtEmployee_ThanhPhoHoKhau.Text = txtEmployee_ThanhPhoKhaiSinh.Text;
            if (txtEmployee_ThanhPhoLienHe.Text == "")
                txtEmployee_ThanhPhoLienHe.Text = txtEmployee_ThanhPhoKhaiSinh.Text;
        }

        private void btnPrint2_Click(object sender, EventArgs e)
        {
            string Type = "InToKhaiBHXH_A02_TS";
            frmPreviewReport f = new frmPreviewReport(_EmployeeID, Type);
            f.Show(this);
        }

        private void txtLuongKhac_Leave(object sender, EventArgs e)
        {
            if (txtOther.Text.Trim().Length == 0)
            {
                txtOther.Text = "0.00";
            }
            else
            {
                txtOther.Text = Convert.ToDecimal(txtOther.Text).ToString("#,##0");
            }
        }

        private void txtPCChucVu_Leave(object sender, EventArgs e)
        {

            if (txtPositionFactor.Text.Trim().Length == 0)
            {
                txtPositionFactor.Text = "0.00";
            }
            else
            {
                txtPositionFactor.Text = Convert.ToDecimal(txtPositionFactor.Text).ToString("#,##0.00");
            }
        }

        private void txtTNVK_Leave(object sender, EventArgs e)
        {
            if (txtOverFactor.Text.Trim().Length == 0)
            {
                txtOverFactor.Text = "0.00";
            }
            else
            {
                txtOverFactor.Text = Convert.ToDecimal(txtOverFactor.Text).ToString(Globals.CS_DISPLAY_RATE_FORMAT);
            }
        }

        private void txtTNNghe_Leave(object sender, EventArgs e)
        {
            if (txtJobFactor.Text.Trim().Length == 0)
            {
                txtJobFactor.Text = "0.00";
            }
            else
            {
                txtJobFactor.Text = Convert.ToDecimal(txtJobFactor.Text).ToString(Globals.CS_DISPLAY_RATE_FORMAT);
            }
        }

        private void txtMucTienBHYT_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_MucTienBHYT.Text.Trim().Length == 0)
            {
                txtEmployee_MucTienBHYT.Text = "0.00";
            }
            else
            {
                txtEmployee_MucTienBHYT.Text = Convert.ToDecimal(txtEmployee_MucTienBHYT.Text).ToString("#,##0");
            }
        }

        private void txtThuNhapBHTN_Leave(object sender, EventArgs e)
        {
            if (txtEmployee_ThuNhapBHTN.Text.Trim().Length == 0)
            {
                txtEmployee_ThuNhapBHTN.Text = "0.00";
            }
            else
            {
                txtEmployee_ThuNhapBHTN.Text = Convert.ToDecimal(txtEmployee_ThuNhapBHTN.Text).ToString("#,##0");
            }
        }

        private void txtLuongChinh_Leave(object sender, EventArgs e)
        {
            if (txtBasicFactor.GetValue<decimal>() > 0)
            {
                txtBasicFactor.Text = Convert.ToDecimal(txtBasicFactor.Text).ToString(Globals.CS_DISPLAY_RATE_FORMAT);
                FormGlobals.Control_SetViewOnly(true, txtBasicSalary);
                FormGlobals.Control_SetViewOnly(false, txtPositionFactor, txtPositionAllowance, txtOverFactor_Percent, txtOverFactor, txtOverAllowance, txtJobFactor, txtJobFactor_Percent, txtJobAllowance);
            }
            else
            {
                txtBasicFactor.Text = "0";
                //
                FormGlobals.Control_SetViewOnly(false, txtBasicSalary);
                FormGlobals.Control_SetViewOnly(true, txtPositionFactor, txtPositionAllowance, txtOverFactor_Percent, txtOverFactor, txtOverAllowance, txtJobFactor, txtJobFactor_Percent, txtJobAllowance);
            }
        }

        private void txtBasicSalary_Leave(object sender, EventArgs e)
        {
            if (txtBasicSalary.GetValue<decimal>() > 0)
            {
                txtBasicSalary.Text = Convert.ToDecimal(txtBasicSalary.Text).ToString(Globals.CS_MONEY_FORMAT);
                FormGlobals.Control_SetViewOnly(true, txtPositionFactor, txtPositionAllowance, txtOverFactor_Percent, txtOverFactor, txtOverAllowance, txtJobFactor, txtJobFactor_Percent, txtJobAllowance);
            }
            else
            {
                txtBasicSalary.Text = "0";
            }
        }

        private void frmNhanSu_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void frmNhanSu_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    break;
                case Keys.Home:
                    btnFirst_Click(null, null);
                    break;
                case Keys.PageUp:
                    btnPrevious_Click(null, null);
                    break;
                case Keys.PageDown:
                    btnNext_Click(null, null);
                    break;
                case Keys.End:
                    btnLast_Click(null, null);
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Kiểm tra hợp lệ số chứng minh thư
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtEmployee_IDCardNo_Validated(object sender, EventArgs e)
        {
            if (txtEmployee_IDCardNo.Text.Length != 9 && txtEmployee_IDCardNo.Text.Length != 12 && txtEmployee_IDCardNo.Text.Length != 0)
            {
                MessageBox.Show("Số chứng minh thư phải đủ 9 hoặc 12 ký tự");
                txtEmployee_IDCardNo.Focus();
                return;
            }
        }

        /// <summary>
        /// Kiểm tra mã bệnh viện
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtEmployee_HospitalRegionCode_Leave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtEmployee_HospitalRegionCode.Text) || String.IsNullOrEmpty(txtMedicalPlace.Text))
                return;
            //
            if (!_wcfHospital.Hospital_CheckExist(txtMedicalPlace.Text.Trim(), txtEmployee_HospitalRegionCode.Text))
            {
                frmHospitalList f = new frmHospitalList(true);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    txtMedicalPlace.Text = f.HospitalCode;
                    txtHospitalName.Text = f.HospitalName;
                    txtEmployee_HospitalRegionCode.Text = f.HospitalRegionCode;
                }
            }
            else
            {
                HospitalInfo hptInfo = _wcfHospital.Hospital_GetInfoByCode(txtMedicalPlace.Text.Trim(), txtEmployee_HospitalRegionCode.Text);
                if (hptInfo != null)
                    txtHospitalName.Text = hptInfo.Name;
            }
        }

        /// <summary>
        /// Kiểm tra bệnh viện
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtMedicalPlace_Leave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtMedicalPlace.Text))
                return;
            //

            if (!_wcfHospital.Hospital_CheckExist(txtMedicalPlace.Text.Trim(), txtEmployee_HospitalRegionCode.Text))
            {
                frmHospitalList f = new frmHospitalList(true);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    txtMedicalPlace.Text = f.HospitalCode;
                    txtHospitalName.Text = f.HospitalName;
                    txtEmployee_HospitalRegionCode.Text = f.HospitalRegionCode;
                }
            }
            else
            {
                HospitalInfo hptInfo = _wcfHospital.Hospital_GetInfoByCode(txtMedicalPlace.Text.Trim(), txtEmployee_HospitalRegionCode.Text);
                if (hptInfo != null)
                    txtHospitalName.Text = hptInfo.Name;
            }
        }



        private void txtOverFactor_Percent_Validated(object sender, EventArgs e)
        {
            if (txtOverFactor.Text.Length == 0)
                txtOverFactor.Text = "0";
            //
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            //decimal BasicSalary = UISetting.GetDecimalNo(txtBasicSalary.Text);
            decimal Percent_OverFactor = UISetting.GetDecimalNo(txtOverFactor_Percent.Text);
            if (BasicFactor != 0)
                txtOverFactor.Text = ((Percent_OverFactor * BasicFactor) / 100).ToString(UIParamater.Cham3So);
            //if (BasicSalary != 0)
            //    txtOverAllowance.Text = ((Percent_OverFactor * BasicSalary) / 100).ToString("#,##0.00");
        }

        private void txtJobFactor_Percent_Validated(object sender, EventArgs e)
        {
            if (txtJobFactor.Text.Length == 0)
                txtJobFactor.Text = "0";
            //
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            decimal PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            decimal Percent_JobFactor = Globals.Object_GetValue<decimal>(txtJobFactor_Percent.Text);
            decimal OverFactor = UISetting.GetDecimalNo(txtOverFactor.Text);
            decimal JobFactor = (BasicFactor + PositionFactor + OverFactor) * Percent_JobFactor / 100;
            //
            //txtDetail_Newoverfactor.Text = OverFactor.ToString(UIParamater.Cham3So);
            txtJobFactor.Text = JobFactor.ToString(UIParamater.Cham3So);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string sContent = "Bạn có chắc muốn xóa dữ liệu: " + _EmployeeID.ToString();
            if (!FormGlobals.Message_Confirm(sContent))
                return;
            //
            FormGlobals.App_ProgressShow(delegate()
            {
                if (_wcfEployee.Employee_Delete(_EmployeeID))
                {
                    LoadData();
                }
            }, "Xóa dữ liệu");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //return;
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                DataTable dt = ExcelHelper.Excel_GetData(filePath, true);

                FormGlobals.App_ProgressShow(delegate()
                {
                    //
                    ContractTypeClient wcf = Service.ContractType();
                    DataTable dtc = wcf.ContractType_GetByBranchCode(Program.strBranchCode).Tables[0];
                    //
                    int contractType = 0;
                    if(dtc.Rows.Count > 0)
                       contractType = Globals.Object_GetValue<int>(dtc.Rows[0]["ContractType_ID"]);
                    //
                    foreach (DataRow dr in dt.Rows)
                    {
                        try
                        {

                           
                            //
                            ImportRow(dr,contractType);
                        }
                        catch (Exception)
                        {
                        }
                    }

                }, "Import...");
            }
        }

        private void ImportRow(DataRow dr, int contractType)
        {
            bool retValLC = true;
            bool retValPD = true;
            bool retVal = true;
            
            //Form2Info
            Employee_Info _employeeInfo = new Employee_Info();
            //
            _employeeInfo.Employee_ID = 0;
            _employeeInfo.Employee_BranchID = Program.intBranchID;
            _employeeInfo.Employee_FirstName = dr["TEN"].ToString();
            _employeeInfo.Employee_LastName = dr["HO VA TEN DEM"].ToString();


            if(String.IsNullOrEmpty(_employeeInfo.Employee_FirstName)
                && String.IsNullOrEmpty(_employeeInfo.Employee_LastName))
                return;
            //

            _employeeInfo.Employee_DateOfBirth =  UISetting.GetDate(dr["NGAY SINH"].ToString());
            
            _employeeInfo.Employee_AddressLH = dr["SO NHA"].ToString() + "|" + dr["XA/PHUONG"] + "|" + dr["QUAN/HUYEN"].ToString() + "|" + dr["TINH/TP"].ToString();
            _employeeInfo.Employee_AddressHK = dr["SO NHA"].ToString() + "|" + dr["XA/PHUONG"] + "|" + dr["QUAN/HUYEN"].ToString() + "|" + dr["TINH/TP"].ToString();
            _employeeInfo.Employee_AddressKS = dr["SO NHA"].ToString() + "|" + dr["XA/PHUONG"] + "|" + dr["QUAN/HUYEN"].ToString() + "|" + dr["TINH/TP"].ToString();
            _employeeInfo.Employee_Phone = dr["DIEN THOAI"].ToString();
            _employeeInfo.Employee_Email = "";
            _employeeInfo.Employee_EthlicID = 1; //Dân tộc kinh
            _employeeInfo.Employee_Nationality = 1;//Việt nam
            _employeeInfo.Employee_MedicalPlace = dr["MA BV KCB"].ToString();
            _employeeInfo.Employee_MedicalRegionCode = "01"; //Hanoi
            _employeeInfo.Employee_MedicalNo = dr["THE BHYT"].ToString(); //the bhyt
            _employeeInfo.Employee_IDCardNo = dr["CMND"].ToString();
            _employeeInfo.Employee_IDCardIssueDate = UISetting.GetDate(dr["NGAY CAP"].ToString());
            _employeeInfo.Employee_IDCardIssueAuthority = dr["NOI CAP"].ToString();

            //_employeeInfo.Employee_SIBookNo = ""; //sổ tạm
            _employeeInfo.Employee_SIBookNo = dr["SO BHXH"].ToString(); //sổ tạm
            _employeeInfo.Employee_Sex = dr["GIOI TINH"].ToString() == "Nam";

            _employeeInfo.Employee_ChaMe = "";
            _employeeInfo.Employee_ThanNhanKhac = "";

            _employeeInfo.Employee_IsQuited = false;
            _employeeInfo.Employee_BranchID = Program.intBranchID;
            //hiepdh: Tinh TNN, TNVK hay ko
            _employeeInfo.Employee_UnOverFactor = false;
            _employeeInfo.Employee_UnJobFactor = false;
            _employeeInfo.Employee_ThuNhapBHTN = 0;
            _employeeInfo.Employee_PhuongThucBHTN = "";
            _employeeInfo.Employee_DoiTuongBHYT = "";
            _employeeInfo.Employee_MucTienBHYT = 0;
            _employeeInfo.Employee_PhuongThucBHYT = "";

            //
            //if (txtEmployee_BookNo.Text.Trim().Length > 0)
            //{
            //    string BranchName = "";
            //    BranchName = _wcfBranch.Branch_GetInfo(_employeeInfo.Employee_BranchID).Name;
            //    if (BranchName != txtBranchName.Text)
            //    {
            //        _employeeInfo.Employee_ID = Employee_Controller.Instance().Employee_InsertNS(_employeeInfo);
            //        _EmployeeID = _employeeInfo.Employee_ID;
            //        if (_EmployeeID > 0)
            //        {
            //            FormLC2Info();
            //            FormPD2Info();
            //            if (_PDID > 0)
            //            {
            //                if (PositionDetail_Controller.Instance().PositionDetail_UpdateNS(_PDInfo))
            //                    retValPD = true;
            //                else
            //                    retValPD = false;
            //            }
            //            else
            //            {
            //                _PDInfo.PD_ID = PositionDetail_Controller.Instance().PositionDetail_InsertNS(_PDInfo);
            //                _PDID = _PDInfo.PD_ID;
            //                if (_PDID > 0)
            //                {
            //                    retValPD = true;
            //                }
            //                else
            //                    retValPD = false;
            //            }
            //            if (_LCID > 0)
            //            {
            //                if (LabourContract_Controller.Instance().LabourContract_UpdateNS(_LabourInfo))
            //                    retValLC = true;
            //                else
            //                    retValLC = false;
            //            }
            //            else
            //            {
            //                _LabourInfo.LC_ID = LabourContract_Controller.Instance().LabourContract_InsertNS(_LabourInfo);
            //                _LCID = _LabourInfo.LC_ID;
            //                if (_LCID > 0)
            //                {
            //                    retValLC = true;
            //                }
            //                else
            //                    retValLC = false;
            //            }
            //            retVal = true;
            //        }
            //        else
            //        {
            //            retVal = false;
            //            MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //            return;
            //        }
            //    }
            //    else
            //    {
            //        if (Employee_Controller.Instance().Employee_UpdateNS(_employeeInfo))
            //        {
            //            FormLC2Info();
            //            if (_LCID > 0)
            //            {
            //                if (LabourContract_Controller.Instance().LabourContract_UpdateNS(_LabourInfo))
            //                    retValLC = true;
            //                else
            //                    retValLC = false;
            //            }
            //            else
            //            {
            //                _LabourInfo.LC_ID = LabourContract_Controller.Instance().LabourContract_InsertNS(_LabourInfo);
            //                _LCID = _LabourInfo.LC_ID;
            //                if (_LCID > 0)
            //                {
            //                    retValLC = true;
            //                }
            //                else
            //                    retValLC = false;
            //            }
            //            FormPD2Info();
            //            if (_PDID > 0)
            //            {
            //                if (PositionDetail_Controller.Instance().PositionDetail_UpdateNS(_PDInfo))
            //                    retValPD = true;
            //                else
            //                    retValPD = false;
            //            }
            //            else
            //            {
            //                _PDInfo.PD_ID = PositionDetail_Controller.Instance().PositionDetail_InsertNS(_PDInfo);
            //                _PDID = _PDInfo.PD_ID;
            //                if (_PDID > 0)
            //                    retValPD = true;
            //                else
            //                    retValPD = false;
            //            }
            //            retVal = true;
            //        }
            //        else
            //        {
            //            retVal = false;
            //            MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //            return;
            //        }

            //    }
            //}
            //else
            //{
                _employeeInfo.Employee_ID = Employee_Controller.Instance().Employee_InsertNS(_employeeInfo);
                _EmployeeID = _employeeInfo.Employee_ID;
                //txtEmployee_BookNo.Text = _employeeInfo.Employee_SIBookNo;
                if (_EmployeeID > 0)
                {
                    retVal = true;

                    
                    //FormLC2Info();
                    _LCID = 0;
                    _LabourInfo = new LabourContract_Info();
                    //
                    _LabourInfo.LC_EmployeeID = _EmployeeID;
                    _LabourInfo.LC_ID = _LCID;
                    _LabourInfo.LC_No = dr["SO HD"].ToString();
                    _LabourInfo.LC_Date = Globals.Object_GetValue<DateTime>(dr["NGAY KY HD"]);
                    _LabourInfo.LC_BeginDate = Globals.Object_GetValue<DateTime>(dr["HIEU LUC HD"]);

                    _LabourInfo.LC_ContractTypeID = contractType;// Globals.Object_GetValue<int>(cboContract_Type.SelectedValue);


                    string phongBan = dr["PHONG BAN"].ToString();
                    string chucVu = dr["CHUC VU"].ToString();
                    

                    _LabourInfo.LC_PositionID = Employee_Controller.Instance().Import_ChucVu(chucVu, Program.strBranchCode);
                    _LabourInfo.LC_DepartmentID = Employee_Controller.Instance().Import_PhongBan(phongBan,Program.intBranchID);


                    //FormPD2Info();
                    _PDID = 0;
                    _PDInfo = new PositionDetail_Info();
                    //
                    _PDInfo.PD_ID = _PDID;
                    _PDInfo.PD_EmployeeID = _EmployeeID;
                    _PDInfo.PD_PositionID = _LabourInfo.LC_PositionID;// 
                    //_PDInfo.PD_DateSalary = txtDateSalary.GetValue<DateTime>();
                    _PDInfo.PD_BeginDate = Globals.Object_GetValue<DateTime>(dr["DONG TU THANG"]);
                    
                    //_PDInfo.PD_DatePC = txtDatePC.GetValue<DateTime>();
                    //_PDInfo.PD_DateTNN = txtDateTNN.GetValue<DateTime>();
                    //_PDInfo.PD_DateTNVK = txtDateTNVK.GetValue<DateTime>();
                    //_PDInfo.PD_BasicFactor = txtBasicFactor.GetValue<decimal>();

                    _PDInfo.PD_BasicSalary = Globals.Object_GetValue<decimal>(dr["LUONG/HS"]);
                    _PDInfo.PD_DepartmentID = _LabourInfo.LC_DepartmentID;
                    //_PDInfo.PD_PositionFactor = txtPositionFactor.GetValue<decimal>();
                    //_PDInfo.PD_PositionAllowance = txtPositionAllowance.GetValue<decimal>();

                    //_PDInfo.PD_OverFactorPercent = txtOverFactor_Percent.GetValue<int>();
                    //_PDInfo.PD_OverFactor = txtOverFactor.GetValue<decimal>();

                    //_PDInfo.PD_OverAllowance = txtOverAllowance.GetValue<decimal>();
                    //_PDInfo.PD_OtherAllowance = txtOther.GetValue<decimal>();
                    ////
                    //_PDInfo.PD_JobFactorPercent = txtJobFactor_Percent.GetValue<int>();
                    //_PDInfo.PD_JobFactor = txtJobFactor.GetValue<decimal>();
                    //_PDInfo.PD_JobAllowance = txtJobAllowance.GetValue<decimal>();

                    
                    if (_PDID > 0)
                    {
                        if (PositionDetail_Controller.Instance().PositionDetail_UpdateNS(_PDInfo))
                            retValPD = true;
                        else
                            retValPD = false;
                    }
                    else
                    {
                        _PDInfo.PD_ID = PositionDetail_Controller.Instance().PositionDetail_InsertNS(_PDInfo);
                        _PDID = _PDInfo.PD_ID;
                        if (_PDID > 0)
                        {
                            retValPD = true;
                        }
                        else
                            retValPD = false;
                    }
                    if (_LCID > 0)
                    {
                        if (LabourContract_Controller.Instance().LabourContract_UpdateNS(_LabourInfo))
                            retValLC = true;
                        else
                            retValLC = false;
                    }
                    else
                    {
                        _LabourInfo.LC_ID = LabourContract_Controller.Instance().LabourContract_InsertNS(_LabourInfo);
                        _LCID = _LabourInfo.LC_ID;
                        if (_LCID > 0)
                        {
                            retValLC = true;
                        }
                        else
                            retValLC = false;
                    }
                }
            //}
        }
    }
}

